Exercises
Window dimensions
Let's use an effect hook to track the window's dimensions over time!
Acceptance Criteria:
- As the window is resized, the numbers shown in the “Result” tab should update, accurately showing the width and height of the iframe.
- Only a single event listener should be registered.
Note: You can test this by dragging the division between the two tabs. If you're not using a pointer device, you can focus the divider and use the left/right arrow keys:
Code Playground
Solution:
Toasty!
In the old Mortal Kombat games, a little fella would occasionally pop out the side of the screen and yell Toasty!, for unclear reasons.
A few years back, I took inspiration from this quirky effect, and used it to slide my 3D mascot out on my blog:
In this exercise, we'll create our own Toasty effect!
It will be tied to scroll position. When the user scrolls to a specific point in the page, a character will slide out from the edge of the screen. We'll use the spiffy IntersectionObserver API to help us out.
There's a lot of stuff in this exercise, and it can seem pretty intimidating. To help you make sense of what's going on, I've prepared a two-minute intro video, to provide some context around how this exercise is meant to work:
Acceptance Criteria:
- As the user scrolls near the bottom of the page, a ghost character should slide in.
- This can be accomplished by observing the
styles.wrapper
element, and setting theisShown
state variable to true/false based on whether it's within the viewport or not. - You shouldn't use
document.querySelector
! You can capture a reference to the wrapper element with theReact.useRef
hook.
Code Playground
Solution: